package com.ruoyi.factorydata.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.factorydata.domain.*;
import com.ruoyi.factorydata.mapper.*;
import com.ruoyi.factorydata.service.*;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Optional;

/**
 * 物料库存流水Service业务层处理
 *
 * @author ruoyi
 * @date 2025-07-23
 */
@Service
public class MaterialInventoryFlowServiceImpl extends ServiceImpl<MaterialInventoryFlowMapper, MaterialInventoryFlow> implements IMaterialInventoryFlowService {
    private final MaterialInventoryFlowMapper materialInventoryFlowMapper;
    private final PurchaseOrderInfoMapper purchaseOrderInfoMapper;
    private final IMaterialUnitDataService materialUnitDataService;
    private final IWarehouseInfoService warehouseInfoService;
    private final IWarehousePositionService warehousePositionService;
    private final IMaterialCategoryService materialCategoryService;
    private final MaterialRequisitionMapper materialRequisitionMapper;
    private final WorkOrderInfoMapper workOrderInfoMapper;
    private final StockOutOrderMapper stockOutOrderMapper;
    public MaterialInventoryFlowServiceImpl(MaterialInventoryFlowMapper materialInventoryFlowMapper,
                                            PurchaseOrderInfoMapper purchaseOrderInfoMapper,
                                            IMaterialUnitDataService materialUnitDataService,
                                            IWarehouseInfoService warehouseInfoService,
                                            IWarehousePositionService warehousePositionService,
                                            IMaterialCategoryService materialCategoryService,
                                            MaterialRequisitionMapper materialRequisitionMapper,
                                            WorkOrderInfoMapper workOrderInfoMapper,
                                            StockOutOrderMapper stockOutOrderMapper) {
        this.materialInventoryFlowMapper = materialInventoryFlowMapper;
        this.purchaseOrderInfoMapper = purchaseOrderInfoMapper;
        this.materialUnitDataService = materialUnitDataService;
        this.warehouseInfoService = warehouseInfoService;
        this.warehousePositionService = warehousePositionService;
        this.materialCategoryService = materialCategoryService;
        this.materialRequisitionMapper = materialRequisitionMapper;
        this.workOrderInfoMapper = workOrderInfoMapper;
        this.stockOutOrderMapper = stockOutOrderMapper;
    }

    /**
     * 查询物料库存流水
     *
     * @param id 物料库存流水主键
     * @return 物料库存流水
     */
    @Override
    public MaterialInventoryFlow selectMaterialInventoryFlowById(Long id) {
        return materialInventoryFlowMapper.selectMaterialInventoryFlowById(id);
    }

    /**
     * 查询物料库存流水列表
     *
     * @param materialInventoryFlow 物料库存流水
     * @return 物料库存流水
     */
    @Override
    public List<MaterialInventoryFlow> selectMaterialInventoryFlowList(MaterialInventoryFlow materialInventoryFlow) {
        HashMap<Long, String> unitNameCache = new HashMap<>();
        HashMap<Long, String> positionNameCache = new HashMap<>();
        HashMap<Long, String> warehouseNameCache = new HashMap<>();
        HashMap<Long, String> categoryNameCache = new HashMap<>();

        List<MaterialInventoryFlow> materialInventoryFlows = materialInventoryFlowMapper.selectMaterialInventoryFlowList(materialInventoryFlow);
        for (MaterialInventoryFlow flow : materialInventoryFlows) {
            String unitName = materialUnitDataService.getByIdAndCache(flow.getMaterialUnit(), unitNameCache);
            String positionName = warehousePositionService.getByIdAndCache(flow.getWarehousePositionId(), positionNameCache);
            String warehouseName = warehouseInfoService.getNameByIdAndCache(flow.getWarehouseId(), warehouseNameCache);
            String categoryName = materialCategoryService.getNameByIdAndCache(flow.getMaterialCategory(), categoryNameCache);
            if(StringUtils.equals(flow.getDocumentType(), "purchase")){
                PurchaseOrderInfo purchaseOrderInfo = purchaseOrderInfoMapper.selectOne(new LambdaQueryWrapper<PurchaseOrderInfo>()
                        .eq(PurchaseOrderInfo::getPurchaseOrderCode, flow.getDocumentNumber())
                        .last("limit 1"));
                flow.setDocumentId(Optional.ofNullable(purchaseOrderInfo).map(PurchaseOrderInfo::getId).orElse(null)+"");
            }else if (StringUtils.equals(flow.getDocumentType(), "requisitionOut")
                    ||StringUtils.equals(flow.getDocumentType(), "requisition_return_in")) {
                MaterialRequisition materialRequisition = materialRequisitionMapper.selectOne(new LambdaQueryWrapper<MaterialRequisition>()
                        .eq(MaterialRequisition::getRequisitionNo, flow.getDocumentNumber())
                        .last("limit 1"));
                flow.setDocumentId(Optional.ofNullable(materialRequisition).map(MaterialRequisition::getId).orElse(null)+"");
            }else if (StringUtils.equals(flow.getDocumentType(), "product_model")
                    ||StringUtils.equals(flow.getDocumentType(), "product_in")) {
                WorkOrderInfo workOrderInfo = workOrderInfoMapper.selectOne(new LambdaQueryWrapper<WorkOrderInfo>()
                        .eq(WorkOrderInfo::getWorkOrderCode, flow.getDocumentNumber())
                        .last("limit 1"));
                flow.setDocumentId(Optional.ofNullable(workOrderInfo).map(WorkOrderInfo::getId).orElse(null)+"");
            }else if (StringUtils.equals(flow.getDocumentType(), "stock_out")) {
                StockOutOrder stockOutOrder = stockOutOrderMapper.selectOne(new LambdaQueryWrapper<StockOutOrder>()
                        .eq(StockOutOrder::getOrderNo, flow.getDocumentNumber())
                        .last("limit 1"));
                flow.setDocumentId(Optional.ofNullable(stockOutOrder).map(StockOutOrder::getId).orElse(null)+"");
            }
            flow.setMaterialUnitName(unitName);
            flow.setWarehousePositionName(positionName);
            flow.setWarehouseName(warehouseName);
            flow.setMaterialCategoryName(categoryName);

        }
        return materialInventoryFlows;
    }

    /**
     * 新增物料库存流水
     *
     * @param materialInventoryFlow 物料库存流水
     * @return 结果
     */
    @Override
    public int insertMaterialInventoryFlow(MaterialInventoryFlow materialInventoryFlow) {
                materialInventoryFlow.setCreateTime(DateUtils.getNowDate());
            return materialInventoryFlowMapper.insertMaterialInventoryFlow(materialInventoryFlow);
    }

    /**
     * 修改物料库存流水
     *
     * @param materialInventoryFlow 物料库存流水
     * @return 结果
     */
    @Override
    public int updateMaterialInventoryFlow(MaterialInventoryFlow materialInventoryFlow) {
                materialInventoryFlow.setUpdateTime(DateUtils.getNowDate());
        return materialInventoryFlowMapper.updateMaterialInventoryFlow(materialInventoryFlow);
    }

    /**
     * 批量删除物料库存流水
     *
     * @param ids 需要删除的物料库存流水主键
     * @return 结果
     */
    @Override
    public int deleteMaterialInventoryFlowByIds(Long[] ids) {
        return materialInventoryFlowMapper.deleteMaterialInventoryFlowByIds(ids);
    }

    /**
     * 删除物料库存流水信息
     *
     * @param id 物料库存流水主键
     * @return 结果
     */
    @Override
    public int deleteMaterialInventoryFlowById(Long id) {
        return materialInventoryFlowMapper.deleteMaterialInventoryFlowById(id);
    }
}